{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "介绍如何在pytorch环境下，使用FGSM算法攻击基于MNIST数据集预训练的CNN/MLP模型。运行该文件前，需要先运行指定文件生成对应的模型：\n",
    "\n",
    "    cd tutorials\n",
    "    python mnist_model_pytorch.py\n",
    "     "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Jupyter notebook中使用Anaconda中的环境需要单独配置，默认情况下使用的是系统默认的Python环境，以使用advbox环境为例。\n",
    "首先在默认系统环境下执行以下命令，安装ipykernel。\n",
    "\n",
    "    conda install ipykernel\n",
    "    conda install -n advbox ipykernel\n",
    "\n",
    "在advbox环境下激活，这样启动后就可以在界面上看到advbox了。\n",
    "\n",
    "    python -m ipykernel install --user --name advbox --display-name advbox \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "train_data: torch.Size([60000, 28, 28])\n",
      "train_labels: torch.Size([60000])\n",
      "test_data: torch.Size([10000, 28, 28])\n"
     ]
    }
   ],
   "source": [
    "#调试开关\n",
    "import logging\n",
    "#logging.basicConfig(level=logging.INFO,format=\"%(filename)s[line:%(lineno)d] %(levelname)s %(message)s\")\n",
    "#logger=logging.getLogger(__name__)\n",
    "import sys\n",
    "import torch\n",
    "import torchvision\n",
    "from torchvision import datasets, transforms\n",
    "from torch.autograd import Variable\n",
    "import torch.utils.data.dataloader as Data\n",
    "from advbox.adversary import Adversary\n",
    "from advbox.attacks.gradient_method import FGSM\n",
    "from advbox.models.pytorch import PytorchModel\n",
    "from tutorials.mnist_model_pytorch import Net"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cpu\n",
      "attack success, original_label=1, adversarial_label=4, count=1\n",
      "attack success, original_label=1, adversarial_label=4, count=2\n",
      "attack success, original_label=0, adversarial_label=6, count=3\n",
      "attack success, original_label=9, adversarial_label=4, count=4\n",
      "attack success, original_label=3, adversarial_label=7, count=5\n",
      "attack success, original_label=1, adversarial_label=7, count=6\n",
      "attack success, original_label=3, adversarial_label=8, count=7\n",
      "attack success, original_label=1, adversarial_label=7, count=8\n",
      "attack success, original_label=4, adversarial_label=9, count=9\n",
      "attack success, original_label=9, adversarial_label=4, count=10\n",
      "attack success, original_label=9, adversarial_label=7, count=11\n",
      "attack success, original_label=9, adversarial_label=3, count=12\n",
      "attack success, original_label=3, adversarial_label=8, count=13\n",
      "attack success, original_label=7, adversarial_label=3, count=14\n",
      "attack success, original_label=0, adversarial_label=7, count=15\n",
      "attack success, original_label=9, adversarial_label=8, count=16\n",
      "attack success, original_label=9, adversarial_label=4, count=17\n",
      "attack success, original_label=5, adversarial_label=7, count=18\n",
      "attack success, original_label=6, adversarial_label=4, count=19\n",
      "attack success, original_label=8, adversarial_label=3, count=20\n",
      "attack success, original_label=5, adversarial_label=9, count=21\n",
      "attack success, original_label=1, adversarial_label=7, count=22\n",
      "attack success, original_label=0, adversarial_label=9, count=23\n",
      "attack success, original_label=0, adversarial_label=7, count=24\n",
      "attack success, original_label=7, adversarial_label=9, count=25\n",
      "attack success, original_label=0, adversarial_label=9, count=26\n",
      "attack success, original_label=9, adversarial_label=4, count=27\n",
      "attack success, original_label=3, adversarial_label=5, count=28\n",
      "attack success, original_label=1, adversarial_label=5, count=29\n",
      "attack success, original_label=4, adversarial_label=8, count=30\n",
      "attack success, original_label=4, adversarial_label=7, count=31\n",
      "attack success, original_label=0, adversarial_label=5, count=32\n",
      "attack success, original_label=8, adversarial_label=3, count=33\n",
      "attack success, original_label=6, adversarial_label=8, count=34\n",
      "attack success, original_label=0, adversarial_label=7, count=35\n",
      "attack success, original_label=3, adversarial_label=8, count=36\n",
      "attack success, original_label=4, adversarial_label=1, count=37\n",
      "attack success, original_label=9, adversarial_label=7, count=38\n",
      "attack success, original_label=9, adversarial_label=4, count=39\n",
      "attack success, original_label=3, adversarial_label=8, count=40\n",
      "attack success, original_label=5, adversarial_label=3, count=41\n",
      "attack success, original_label=2, adversarial_label=8, count=42\n",
      "attack success, original_label=9, adversarial_label=4, count=43\n",
      "attack success, original_label=4, adversarial_label=7, count=44\n",
      "attack success, original_label=8, adversarial_label=3, count=45\n",
      "attack success, original_label=5, adversarial_label=3, count=46\n",
      "attack success, original_label=9, adversarial_label=4, count=47\n",
      "attack success, original_label=6, adversarial_label=0, count=48\n",
      "attack success, original_label=6, adversarial_label=0, count=49\n",
      "attack success, original_label=5, adversarial_label=9, count=50\n",
      "attack success, original_label=4, adversarial_label=9, count=51\n",
      "attack success, original_label=4, adversarial_label=7, count=52\n",
      "attack success, original_label=3, adversarial_label=7, count=53\n",
      "attack success, original_label=0, adversarial_label=5, count=54\n",
      "attack success, original_label=4, adversarial_label=6, count=55\n",
      "attack success, original_label=5, adversarial_label=3, count=56\n",
      "attack success, original_label=5, adversarial_label=3, count=57\n",
      "attack success, original_label=1, adversarial_label=8, count=58\n",
      "attack success, original_label=5, adversarial_label=3, count=59\n",
      "attack success, original_label=5, adversarial_label=6, count=60\n",
      "attack success, original_label=5, adversarial_label=9, count=61\n",
      "attack success, original_label=2, adversarial_label=3, count=62\n",
      "attack success, original_label=4, adversarial_label=8, count=63\n",
      "attack success, original_label=0, adversarial_label=5, count=64\n",
      "attack success, original_label=5, adversarial_label=9, count=65\n",
      "attack success, original_label=8, adversarial_label=5, count=66\n",
      "attack success, original_label=0, adversarial_label=2, count=67\n",
      "attack success, original_label=2, adversarial_label=3, count=68\n",
      "attack success, original_label=6, adversarial_label=0, count=69\n",
      "attack success, original_label=3, adversarial_label=8, count=70\n",
      "attack success, original_label=0, adversarial_label=5, count=71\n",
      "attack success, original_label=4, adversarial_label=9, count=72\n",
      "attack success, original_label=2, adversarial_label=3, count=73\n",
      "attack success, original_label=0, adversarial_label=5, count=74\n",
      "attack success, original_label=7, adversarial_label=9, count=75\n",
      "attack success, original_label=9, adversarial_label=4, count=76\n",
      "attack success, original_label=1, adversarial_label=4, count=77\n",
      "attack success, original_label=0, adversarial_label=8, count=78\n",
      "attack success, original_label=6, adversarial_label=8, count=79\n",
      "attack success, original_label=0, adversarial_label=9, count=80\n",
      "attack success, original_label=3, adversarial_label=5, count=81\n",
      "attack success, original_label=3, adversarial_label=8, count=82\n",
      "attack success, original_label=1, adversarial_label=7, count=83\n",
      "attack success, original_label=0, adversarial_label=9, count=84\n",
      "attack success, original_label=9, adversarial_label=3, count=85\n",
      "attack success, original_label=6, adversarial_label=8, count=86\n",
      "attack success, original_label=3, adversarial_label=5, count=87\n",
      "attack success, original_label=6, adversarial_label=0, count=88\n",
      "attack success, original_label=4, adversarial_label=7, count=89\n",
      "attack success, original_label=6, adversarial_label=5, count=90\n",
      "attack success, original_label=7, adversarial_label=3, count=91\n",
      "attack success, original_label=9, adversarial_label=7, count=92\n",
      "attack success, original_label=7, adversarial_label=3, count=93\n",
      "attack success, original_label=4, adversarial_label=7, count=94\n",
      "attack success, original_label=3, adversarial_label=5, count=95\n",
      "attack success, original_label=8, adversarial_label=3, count=96\n",
      "attack success, original_label=7, adversarial_label=3, count=97\n",
      "attack success, original_label=1, adversarial_label=7, count=98\n",
      "attack success, original_label=4, adversarial_label=8, count=99\n",
      "attack success, original_label=3, adversarial_label=8, count=100\n",
      "attack success, original_label=0, adversarial_label=9, count=101\n",
      "attack success, original_label=8, adversarial_label=3, count=102\n",
      "attack success, original_label=0, adversarial_label=8, count=103\n",
      "attack success, original_label=6, adversarial_label=8, count=104\n",
      "attack success, original_label=7, adversarial_label=3, count=105\n",
      "attack success, original_label=5, adversarial_label=3, count=106\n",
      "attack success, original_label=3, adversarial_label=5, count=107\n",
      "attack success, original_label=4, adversarial_label=5, count=108\n",
      "attack success, original_label=0, adversarial_label=2, count=109\n",
      "attack success, original_label=9, adversarial_label=4, count=110\n",
      "attack success, original_label=8, adversarial_label=9, count=111\n",
      "attack success, original_label=0, adversarial_label=9, count=112\n",
      "attack success, original_label=8, adversarial_label=5, count=113\n",
      "attack success, original_label=8, adversarial_label=3, count=114\n",
      "attack success, original_label=9, adversarial_label=4, count=115\n",
      "attack success, original_label=3, adversarial_label=5, count=116\n",
      "attack success, original_label=6, adversarial_label=8, count=117\n",
      "attack success, original_label=5, adversarial_label=3, count=118\n",
      "attack success, original_label=7, adversarial_label=9, count=119\n",
      "attack success, original_label=5, adversarial_label=3, count=120\n",
      "attack success, original_label=7, adversarial_label=3, count=121\n",
      "attack success, original_label=0, adversarial_label=8, count=122\n",
      "attack success, original_label=7, adversarial_label=3, count=123\n",
      "attack success, original_label=7, adversarial_label=3, count=124\n",
      "attack success, original_label=6, adversarial_label=0, count=125\n",
      "attack success, original_label=6, adversarial_label=5, count=126\n",
      "attack success, original_label=0, adversarial_label=5, count=127\n",
      "attack success, original_label=6, adversarial_label=4, count=128\n",
      "attack success, original_label=6, adversarial_label=0, count=129\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "attack success, original_label=6, adversarial_label=8, count=130\n",
      "attack success, original_label=1, adversarial_label=7, count=131\n",
      "attack success, original_label=8, adversarial_label=3, count=132\n",
      "attack success, original_label=3, adversarial_label=9, count=133\n",
      "attack success, original_label=8, adversarial_label=2, count=134\n",
      "attack success, original_label=0, adversarial_label=1, count=135\n",
      "attack success, original_label=8, adversarial_label=3, count=136\n",
      "attack success, original_label=7, adversarial_label=9, count=137\n",
      "attack success, original_label=2, adversarial_label=3, count=138\n",
      "attack success, original_label=3, adversarial_label=8, count=139\n",
      "attack success, original_label=8, adversarial_label=3, count=140\n",
      "attack success, original_label=6, adversarial_label=8, count=141\n",
      "attack success, original_label=7, adversarial_label=9, count=142\n",
      "attack success, original_label=6, adversarial_label=8, count=143\n",
      "attack success, original_label=3, adversarial_label=7, count=144\n",
      "attack success, original_label=3, adversarial_label=9, count=145\n",
      "attack success, original_label=2, adversarial_label=1, count=146\n",
      "attack success, original_label=2, adversarial_label=3, count=147\n",
      "attack success, original_label=5, adversarial_label=8, count=148\n",
      "attack success, original_label=4, adversarial_label=9, count=149\n",
      "attack success, original_label=8, adversarial_label=5, count=150\n",
      "attack success, original_label=1, adversarial_label=4, count=151\n",
      "attack success, original_label=6, adversarial_label=8, count=152\n",
      "attack success, original_label=0, adversarial_label=6, count=153\n",
      "attack success, original_label=8, adversarial_label=3, count=154\n",
      "attack success, original_label=3, adversarial_label=8, count=155\n",
      "attack success, original_label=7, adversarial_label=5, count=156\n",
      "attack success, original_label=7, adversarial_label=9, count=157\n",
      "attack success, original_label=8, adversarial_label=3, count=158\n",
      "attack success, original_label=2, adversarial_label=3, count=159\n",
      "attack success, original_label=3, adversarial_label=5, count=160\n",
      "attack success, original_label=6, adversarial_label=0, count=161\n",
      "attack success, original_label=8, adversarial_label=3, count=162\n",
      "attack success, original_label=2, adversarial_label=3, count=163\n",
      "attack success, original_label=5, adversarial_label=8, count=164\n",
      "attack success, original_label=9, adversarial_label=7, count=165\n",
      "attack success, original_label=3, adversarial_label=5, count=166\n",
      "attack success, original_label=1, adversarial_label=7, count=167\n",
      "attack success, original_label=2, adversarial_label=1, count=168\n",
      "attack success, original_label=1, adversarial_label=2, count=169\n",
      "attack success, original_label=1, adversarial_label=7, count=170\n",
      "attack success, original_label=3, adversarial_label=8, count=171\n",
      "attack success, original_label=2, adversarial_label=3, count=172\n",
      "attack success, original_label=6, adversarial_label=8, count=173\n",
      "attack success, original_label=1, adversarial_label=7, count=174\n",
      "attack success, original_label=0, adversarial_label=7, count=175\n",
      "attack success, original_label=0, adversarial_label=7, count=176\n",
      "attack success, original_label=3, adversarial_label=7, count=177\n",
      "attack success, original_label=6, adversarial_label=5, count=178\n",
      "attack success, original_label=1, adversarial_label=7, count=179\n",
      "attack success, original_label=6, adversarial_label=8, count=180\n",
      "attack success, original_label=3, adversarial_label=5, count=181\n",
      "attack success, original_label=5, adversarial_label=9, count=182\n",
      "attack success, original_label=4, adversarial_label=7, count=183\n",
      "attack success, original_label=2, adversarial_label=7, count=184\n",
      "attack success, original_label=5, adversarial_label=2, count=185\n",
      "attack success, original_label=2, adversarial_label=7, count=186\n",
      "attack success, original_label=2, adversarial_label=3, count=187\n",
      "attack success, original_label=6, adversarial_label=0, count=188\n",
      "attack success, original_label=9, adversarial_label=4, count=189\n",
      "attack success, original_label=2, adversarial_label=3, count=190\n",
      "attack success, original_label=1, adversarial_label=7, count=191\n",
      "attack success, original_label=2, adversarial_label=1, count=192\n",
      "attack success, original_label=6, adversarial_label=8, count=193\n",
      "attack success, original_label=6, adversarial_label=0, count=194\n",
      "attack success, original_label=1, adversarial_label=7, count=195\n",
      "attack success, original_label=1, adversarial_label=7, count=196\n",
      "attack success, original_label=4, adversarial_label=9, count=197\n",
      "attack success, original_label=5, adversarial_label=3, count=198\n",
      "attack success, original_label=3, adversarial_label=5, count=199\n",
      "attack success, original_label=1, adversarial_label=7, count=200\n",
      "attack success, original_label=6, adversarial_label=0, count=201\n",
      "attack success, original_label=7, adversarial_label=3, count=202\n",
      "attack success, original_label=3, adversarial_label=5, count=203\n",
      "attack success, original_label=8, adversarial_label=3, count=204\n",
      "attack success, original_label=7, adversarial_label=3, count=205\n",
      "attack success, original_label=2, adversarial_label=3, count=206\n",
      "attack success, original_label=4, adversarial_label=7, count=207\n",
      "attack success, original_label=2, adversarial_label=1, count=208\n",
      "attack success, original_label=2, adversarial_label=3, count=209\n",
      "attack success, original_label=6, adversarial_label=0, count=210\n",
      "attack success, original_label=8, adversarial_label=3, count=211\n",
      "attack success, original_label=4, adversarial_label=8, count=212\n",
      "attack success, original_label=4, adversarial_label=8, count=213\n",
      "attack success, original_label=7, adversarial_label=9, count=214\n",
      "attack success, original_label=7, adversarial_label=2, count=215\n",
      "attack success, original_label=1, adversarial_label=7, count=216\n",
      "attack success, original_label=8, adversarial_label=3, count=217\n",
      "attack success, original_label=4, adversarial_label=7, count=218\n",
      "attack success, original_label=0, adversarial_label=9, count=219\n",
      "attack success, original_label=8, adversarial_label=3, count=220\n",
      "attack success, original_label=0, adversarial_label=9, count=221\n",
      "attack success, original_label=0, adversarial_label=6, count=222\n",
      "attack success, original_label=9, adversarial_label=4, count=223\n",
      "attack success, original_label=4, adversarial_label=8, count=224\n",
      "attack success, original_label=2, adversarial_label=3, count=225\n",
      "attack success, original_label=7, adversarial_label=3, count=226\n",
      "attack success, original_label=4, adversarial_label=3, count=227\n",
      "attack success, original_label=7, adversarial_label=3, count=228\n",
      "attack success, original_label=4, adversarial_label=7, count=229\n",
      "attack success, original_label=7, adversarial_label=3, count=230\n",
      "attack success, original_label=1, adversarial_label=7, count=231\n",
      "attack success, original_label=2, adversarial_label=7, count=232\n",
      "attack success, original_label=0, adversarial_label=9, count=233\n",
      "attack success, original_label=0, adversarial_label=5, count=234\n",
      "attack success, original_label=0, adversarial_label=9, count=235\n",
      "attack success, original_label=2, adversarial_label=3, count=236\n",
      "attack success, original_label=5, adversarial_label=8, count=237\n",
      "attack success, original_label=8, adversarial_label=3, count=238\n",
      "attack success, original_label=6, adversarial_label=5, count=239\n",
      "attack success, original_label=0, adversarial_label=5, count=240\n",
      "attack success, original_label=3, adversarial_label=5, count=241\n",
      "attack success, original_label=9, adversarial_label=4, count=242\n",
      "attack success, original_label=1, adversarial_label=7, count=243\n",
      "attack success, original_label=1, adversarial_label=8, count=244\n",
      "attack success, original_label=3, adversarial_label=5, count=245\n",
      "attack success, original_label=1, adversarial_label=7, count=246\n",
      "attack success, original_label=8, adversarial_label=3, count=247\n",
      "attack success, original_label=1, adversarial_label=7, count=248\n",
      "attack success, original_label=5, adversarial_label=3, count=249\n",
      "attack success, original_label=1, adversarial_label=4, count=250\n",
      "attack success, original_label=7, adversarial_label=3, count=251\n",
      "attack success, original_label=6, adversarial_label=8, count=252\n",
      "attack success, original_label=3, adversarial_label=5, count=253\n",
      "attack success, original_label=3, adversarial_label=9, count=254\n",
      "attack success, original_label=6, adversarial_label=8, count=255\n",
      "attack success, original_label=4, adversarial_label=2, count=256\n",
      "attack success, original_label=8, adversarial_label=3, count=257\n",
      "attack success, original_label=1, adversarial_label=7, count=258\n",
      "attack success, original_label=0, adversarial_label=9, count=259\n",
      "attack success, original_label=8, adversarial_label=3, count=260\n",
      "attack success, original_label=0, adversarial_label=5, count=261\n",
      "attack success, original_label=9, adversarial_label=7, count=262\n",
      "attack success, original_label=9, adversarial_label=5, count=263\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "attack success, original_label=5, adversarial_label=3, count=264\n",
      "attack success, original_label=0, adversarial_label=5, count=265\n",
      "attack success, original_label=5, adversarial_label=3, count=266\n",
      "attack success, original_label=8, adversarial_label=3, count=267\n",
      "attack success, original_label=7, adversarial_label=9, count=268\n",
      "attack success, original_label=7, adversarial_label=3, count=269\n",
      "attack success, original_label=1, adversarial_label=4, count=270\n",
      "attack success, original_label=2, adversarial_label=3, count=271\n",
      "attack success, original_label=3, adversarial_label=5, count=272\n",
      "attack success, original_label=9, adversarial_label=4, count=273\n",
      "attack success, original_label=7, adversarial_label=3, count=274\n",
      "attack success, original_label=9, adversarial_label=7, count=275\n",
      "attack success, original_label=1, adversarial_label=7, count=276\n",
      "attack success, original_label=6, adversarial_label=0, count=277\n",
      "attack success, original_label=6, adversarial_label=0, count=278\n",
      "attack success, original_label=1, adversarial_label=7, count=279\n",
      "attack success, original_label=6, adversarial_label=5, count=280\n",
      "attack success, original_label=1, adversarial_label=4, count=281\n",
      "attack success, original_label=1, adversarial_label=7, count=282\n",
      "attack success, original_label=3, adversarial_label=9, count=283\n",
      "attack success, original_label=9, adversarial_label=4, count=284\n",
      "attack success, original_label=5, adversarial_label=3, count=285\n",
      "attack success, original_label=1, adversarial_label=7, count=286\n",
      "attack success, original_label=0, adversarial_label=4, count=287\n",
      "attack success, original_label=6, adversarial_label=8, count=288\n",
      "attack success, original_label=5, adversarial_label=9, count=289\n",
      "attack success, original_label=3, adversarial_label=5, count=290\n",
      "attack success, original_label=5, adversarial_label=6, count=291\n",
      "attack success, original_label=4, adversarial_label=8, count=292\n",
      "attack success, original_label=1, adversarial_label=7, count=293\n",
      "attack success, original_label=0, adversarial_label=2, count=294\n",
      "attack success, original_label=9, adversarial_label=3, count=295\n",
      "attack success, original_label=9, adversarial_label=4, count=296\n",
      "attack success, original_label=3, adversarial_label=8, count=297\n",
      "attack success, original_label=1, adversarial_label=7, count=298\n",
      "attack success, original_label=1, adversarial_label=7, count=299\n",
      "attack success, original_label=7, adversarial_label=9, count=300\n",
      "attack success, original_label=8, adversarial_label=3, count=301\n",
      "attack success, original_label=0, adversarial_label=9, count=302\n",
      "attack success, original_label=3, adversarial_label=5, count=303\n",
      "attack success, original_label=4, adversarial_label=7, count=304\n",
      "attack success, original_label=8, adversarial_label=3, count=305\n",
      "attack success, original_label=3, adversarial_label=9, count=306\n",
      "attack success, original_label=5, adversarial_label=3, count=307\n",
      "attack success, original_label=8, adversarial_label=3, count=308\n",
      "attack success, original_label=4, adversarial_label=8, count=309\n",
      "attack success, original_label=6, adversarial_label=8, count=310\n",
      "attack success, original_label=5, adversarial_label=6, count=311\n",
      "attack success, original_label=5, adversarial_label=3, count=312\n",
      "attack success, original_label=3, adversarial_label=5, count=313\n",
      "attack success, original_label=3, adversarial_label=2, count=314\n",
      "attack success, original_label=2, adversarial_label=8, count=315\n",
      "attack success, original_label=0, adversarial_label=5, count=316\n",
      "attack success, original_label=3, adversarial_label=8, count=317\n",
      "attack success, original_label=3, adversarial_label=7, count=318\n",
      "attack success, original_label=0, adversarial_label=3, count=319\n",
      "attack success, original_label=9, adversarial_label=3, count=320\n",
      "attack success, original_label=1, adversarial_label=7, count=321\n",
      "attack success, original_label=2, adversarial_label=1, count=322\n",
      "attack success, original_label=6, adversarial_label=0, count=323\n",
      "attack success, original_label=6, adversarial_label=0, count=324\n",
      "attack success, original_label=2, adversarial_label=3, count=325\n",
      "attack success, original_label=9, adversarial_label=7, count=326\n",
      "attack success, original_label=3, adversarial_label=8, count=327\n",
      "attack success, original_label=1, adversarial_label=8, count=328\n",
      "attack success, original_label=9, adversarial_label=7, count=329\n",
      "attack success, original_label=6, adversarial_label=0, count=330\n",
      "attack success, original_label=8, adversarial_label=3, count=331\n",
      "attack success, original_label=4, adversarial_label=2, count=332\n",
      "attack success, original_label=0, adversarial_label=8, count=333\n",
      "attack success, original_label=8, adversarial_label=2, count=334\n",
      "attack success, original_label=3, adversarial_label=5, count=335\n",
      "attack success, original_label=7, adversarial_label=1, count=336\n",
      "attack success, original_label=5, adversarial_label=8, count=337\n",
      "attack success, original_label=1, adversarial_label=4, count=338\n",
      "attack success, original_label=1, adversarial_label=7, count=339\n",
      "attack success, original_label=3, adversarial_label=5, count=340\n",
      "attack success, original_label=5, adversarial_label=3, count=341\n",
      "attack success, original_label=2, adversarial_label=3, count=342\n",
      "attack success, original_label=9, adversarial_label=4, count=343\n",
      "attack success, original_label=5, adversarial_label=3, count=344\n",
      "attack success, original_label=0, adversarial_label=5, count=345\n",
      "attack success, original_label=3, adversarial_label=5, count=346\n",
      "attack success, original_label=7, adversarial_label=3, count=347\n",
      "attack success, original_label=6, adversarial_label=0, count=348\n",
      "attack success, original_label=0, adversarial_label=6, count=349\n",
      "attack success, original_label=0, adversarial_label=9, count=350\n",
      "attack success, original_label=1, adversarial_label=8, count=351\n",
      "attack success, original_label=3, adversarial_label=8, count=352\n",
      "attack success, original_label=9, adversarial_label=7, count=353\n",
      "attack success, original_label=1, adversarial_label=7, count=354\n",
      "attack success, original_label=0, adversarial_label=7, count=355\n",
      "attack success, original_label=1, adversarial_label=4, count=356\n",
      "attack success, original_label=1, adversarial_label=4, count=357\n",
      "attack success, original_label=9, adversarial_label=4, count=358\n",
      "attack success, original_label=8, adversarial_label=3, count=359\n",
      "attack success, original_label=7, adversarial_label=3, count=360\n",
      "attack success, original_label=9, adversarial_label=8, count=361\n",
      "attack success, original_label=2, adversarial_label=3, count=362\n",
      "attack success, original_label=7, adversarial_label=9, count=363\n",
      "attack success, original_label=8, adversarial_label=3, count=364\n",
      "attack success, original_label=9, adversarial_label=4, count=365\n",
      "attack success, original_label=2, adversarial_label=3, count=366\n",
      "attack success, original_label=7, adversarial_label=3, count=367\n",
      "attack success, original_label=8, adversarial_label=3, count=368\n",
      "attack success, original_label=8, adversarial_label=5, count=369\n",
      "attack success, original_label=9, adversarial_label=4, count=370\n",
      "attack success, original_label=4, adversarial_label=7, count=371\n",
      "attack success, original_label=8, adversarial_label=3, count=372\n",
      "attack success, original_label=2, adversarial_label=3, count=373\n",
      "attack success, original_label=5, adversarial_label=3, count=374\n",
      "attack success, original_label=7, adversarial_label=3, count=375\n",
      "attack success, original_label=6, adversarial_label=8, count=376\n",
      "attack success, original_label=1, adversarial_label=7, count=377\n",
      "attack success, original_label=9, adversarial_label=4, count=378\n",
      "attack success, original_label=8, adversarial_label=3, count=379\n",
      "attack success, original_label=5, adversarial_label=3, count=380\n",
      "attack success, original_label=0, adversarial_label=6, count=381\n",
      "attack success, original_label=1, adversarial_label=7, count=382\n",
      "attack success, original_label=2, adversarial_label=3, count=383\n",
      "attack success, original_label=7, adversarial_label=3, count=384\n",
      "attack success, original_label=2, adversarial_label=3, count=385\n",
      "attack success, original_label=9, adversarial_label=3, count=386\n",
      "attack success, original_label=5, adversarial_label=9, count=387\n",
      "attack success, original_label=6, adversarial_label=8, count=388\n",
      "attack success, original_label=6, adversarial_label=0, count=389\n",
      "attack success, original_label=4, adversarial_label=9, count=390\n",
      "attack success, original_label=1, adversarial_label=7, count=391\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "attack success, original_label=4, adversarial_label=3, count=392\n",
      "attack success, original_label=0, adversarial_label=7, count=393\n",
      "attack success, original_label=2, adversarial_label=3, count=394\n",
      "attack success, original_label=4, adversarial_label=5, count=395\n",
      "attack success, original_label=8, adversarial_label=3, count=396\n",
      "attack success, original_label=0, adversarial_label=6, count=397\n",
      "attack success, original_label=7, adversarial_label=3, count=398\n",
      "attack success, original_label=2, adversarial_label=8, count=399\n",
      "attack success, original_label=0, adversarial_label=9, count=400\n",
      "attack success, original_label=0, adversarial_label=9, count=401\n",
      "attack success, original_label=7, adversarial_label=3, count=402\n",
      "attack success, original_label=5, adversarial_label=9, count=403\n",
      "attack success, original_label=8, adversarial_label=5, count=404\n",
      "attack success, original_label=7, adversarial_label=3, count=405\n",
      "attack success, original_label=3, adversarial_label=8, count=406\n",
      "attack success, original_label=1, adversarial_label=4, count=407\n",
      "attack success, original_label=4, adversarial_label=7, count=408\n",
      "attack success, original_label=8, adversarial_label=3, count=409\n",
      "attack success, original_label=0, adversarial_label=9, count=410\n",
      "attack success, original_label=6, adversarial_label=8, count=411\n",
      "attack success, original_label=1, adversarial_label=7, count=412\n",
      "attack success, original_label=8, adversarial_label=3, count=413\n",
      "attack success, original_label=1, adversarial_label=7, count=414\n",
      "attack success, original_label=4, adversarial_label=9, count=415\n",
      "attack success, original_label=7, adversarial_label=3, count=416\n",
      "attack success, original_label=1, adversarial_label=7, count=417\n",
      "attack success, original_label=3, adversarial_label=9, count=418\n",
      "attack success, original_label=1, adversarial_label=7, count=419\n",
      "attack success, original_label=0, adversarial_label=9, count=420\n",
      "attack success, original_label=1, adversarial_label=7, count=421\n",
      "attack success, original_label=0, adversarial_label=9, count=422\n",
      "attack success, original_label=4, adversarial_label=9, count=423\n",
      "attack success, original_label=7, adversarial_label=3, count=424\n",
      "attack success, original_label=7, adversarial_label=9, count=425\n",
      "attack success, original_label=0, adversarial_label=4, count=426\n",
      "attack success, original_label=8, adversarial_label=0, count=427\n",
      "attack success, original_label=0, adversarial_label=8, count=428\n",
      "attack success, original_label=9, adversarial_label=5, count=429\n",
      "attack success, original_label=8, adversarial_label=5, count=430\n",
      "attack success, original_label=5, adversarial_label=3, count=431\n",
      "attack success, original_label=2, adversarial_label=3, count=432\n",
      "attack success, original_label=9, adversarial_label=4, count=433\n",
      "attack success, original_label=3, adversarial_label=7, count=434\n",
      "attack success, original_label=6, adversarial_label=8, count=435\n",
      "attack success, original_label=4, adversarial_label=8, count=436\n",
      "attack success, original_label=8, adversarial_label=4, count=437\n",
      "attack success, original_label=9, adversarial_label=3, count=438\n",
      "attack success, original_label=8, adversarial_label=3, count=439\n",
      "attack success, original_label=0, adversarial_label=9, count=440\n",
      "attack success, original_label=3, adversarial_label=5, count=441\n",
      "attack success, original_label=5, adversarial_label=9, count=442\n",
      "attack success, original_label=3, adversarial_label=7, count=443\n",
      "attack success, original_label=6, adversarial_label=5, count=444\n",
      "attack success, original_label=5, adversarial_label=3, count=445\n",
      "attack success, original_label=0, adversarial_label=9, count=446\n",
      "attack success, original_label=8, adversarial_label=0, count=447\n",
      "attack success, original_label=1, adversarial_label=7, count=448\n",
      "attack success, original_label=9, adversarial_label=7, count=449\n",
      "attack success, original_label=1, adversarial_label=3, count=450\n",
      "attack success, original_label=8, adversarial_label=2, count=451\n",
      "attack success, original_label=1, adversarial_label=8, count=452\n",
      "attack success, original_label=6, adversarial_label=8, count=453\n",
      "attack success, original_label=2, adversarial_label=3, count=454\n",
      "attack success, original_label=0, adversarial_label=9, count=455\n",
      "attack success, original_label=8, adversarial_label=3, count=456\n",
      "attack success, original_label=1, adversarial_label=8, count=457\n",
      "attack success, original_label=1, adversarial_label=7, count=458\n",
      "attack success, original_label=3, adversarial_label=9, count=459\n",
      "attack success, original_label=3, adversarial_label=5, count=460\n",
      "attack success, original_label=7, adversarial_label=2, count=461\n",
      "attack success, original_label=7, adversarial_label=5, count=462\n",
      "attack success, original_label=7, adversarial_label=9, count=463\n",
      "attack success, original_label=7, adversarial_label=3, count=464\n",
      "attack success, original_label=6, adversarial_label=5, count=465\n",
      "attack success, original_label=8, adversarial_label=3, count=466\n",
      "attack success, original_label=7, adversarial_label=1, count=467\n",
      "attack success, original_label=3, adversarial_label=5, count=468\n",
      "attack success, original_label=1, adversarial_label=7, count=469\n",
      "attack success, original_label=8, adversarial_label=3, count=470\n",
      "attack success, original_label=6, adversarial_label=0, count=471\n",
      "attack success, original_label=7, adversarial_label=9, count=472\n",
      "attack success, original_label=4, adversarial_label=3, count=473\n",
      "attack success, original_label=2, adversarial_label=7, count=474\n",
      "attack success, original_label=6, adversarial_label=0, count=475\n",
      "attack success, original_label=3, adversarial_label=5, count=476\n",
      "attack success, original_label=8, adversarial_label=3, count=477\n",
      "attack success, original_label=7, adversarial_label=3, count=478\n",
      "attack success, original_label=0, adversarial_label=9, count=479\n",
      "attack success, original_label=7, adversarial_label=3, count=480\n",
      "attack success, original_label=9, adversarial_label=4, count=481\n",
      "attack success, original_label=1, adversarial_label=7, count=482\n",
      "attack success, original_label=5, adversarial_label=3, count=483\n",
      "attack success, original_label=7, adversarial_label=9, count=484\n",
      "attack success, original_label=6, adversarial_label=8, count=485\n",
      "attack success, original_label=9, adversarial_label=4, count=486\n",
      "attack success, original_label=6, adversarial_label=0, count=487\n",
      "attack success, original_label=0, adversarial_label=7, count=488\n",
      "attack success, original_label=1, adversarial_label=3, count=489\n",
      "attack success, original_label=9, adversarial_label=4, count=490\n",
      "attack success, original_label=7, adversarial_label=3, count=491\n",
      "attack success, original_label=7, adversarial_label=9, count=492\n",
      "attack success, original_label=3, adversarial_label=7, count=493\n",
      "attack success, original_label=6, adversarial_label=8, count=494\n",
      "attack success, original_label=6, adversarial_label=5, count=495\n",
      "attack success, original_label=9, adversarial_label=4, count=496\n",
      "attack success, original_label=6, adversarial_label=0, count=497\n",
      "attack success, original_label=3, adversarial_label=7, count=498\n",
      "attack success, original_label=7, adversarial_label=3, count=499\n",
      "attack success, original_label=6, adversarial_label=0, count=500\n",
      "[TEST_DATASET]: fooling_count=500, total_count=500, fooling_rate=1.000000\n",
      "fgsm attack done\n"
     ]
    }
   ],
   "source": [
    "TOTAL_NUM = 500\n",
    "pretrained_model=\"tutorials/mnist-pytorch/net.pth\"\n",
    "loss_func = torch.nn.CrossEntropyLoss()\n",
    "\n",
    "#使用MNIST测试数据集 随机挑选TOTAL_NUM个\n",
    "test_loader = torch.utils.data.DataLoader(\n",
    "    datasets.MNIST('tutorials/mnist-pytorch/data', train=False, download=True, transform=transforms.Compose([\n",
    "        transforms.ToTensor(),\n",
    "    ])),\n",
    "    batch_size=1, shuffle=True)\n",
    "\n",
    "# Define what device we are using\n",
    "logging.info(\"CUDA Available: {}\".format(torch.cuda.is_available()))\n",
    "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
    "\n",
    "# Initialize the network\n",
    "model = Net().to(device)\n",
    "\n",
    "# Load the pretrained model\n",
    "model.load_state_dict(torch.load(pretrained_model, map_location='cpu'))\n",
    "\n",
    "# Set the model in evaluation mode. In this case this is for the Dropout layers\n",
    "model.eval()\n",
    "\n",
    "# advbox demo\n",
    "m = PytorchModel(\n",
    "    model, loss_func,(0, 1),\n",
    "    channel_axis=1)\n",
    "\n",
    "#实例化FGSM\n",
    "attack = FGSM(m)\n",
    "#设置攻击步长为0.1\n",
    "attack_config = {\"epsilons\": 0.1}\n",
    "\n",
    "# use test data to generate adversarial examples\n",
    "total_count = 0\n",
    "fooling_count = 0\n",
    "\n",
    "for i, data in enumerate(test_loader):\n",
    "    inputs, labels = data\n",
    "    inputs, labels=inputs.numpy(),labels.numpy()\n",
    "\n",
    "    total_count += 1\n",
    "    adversary = Adversary(inputs, labels[0])\n",
    "\n",
    "    # FGSM non-targeted attack\n",
    "    adversary = attack(adversary, **attack_config)\n",
    "\n",
    "    if adversary.is_successful():\n",
    "        fooling_count += 1\n",
    "        print(\n",
    "            'attack success, original_label=%d, adversarial_label=%d, count=%d'\n",
    "            % (labels, adversary.adversarial_label, total_count))\n",
    "\n",
    "    else:\n",
    "        print('attack failed, original_label=%d, count=%d' %\n",
    "              (labels, total_count))\n",
    "\n",
    "    if total_count >= TOTAL_NUM:\n",
    "        print(\n",
    "            \"[TEST_DATASET]: fooling_count=%d, total_count=%d, fooling_rate=%f\"\n",
    "            % (fooling_count, total_count,\n",
    "               float(fooling_count) / total_count))\n",
    "        break\n",
    "print(\"fgsm attack done\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "advbox",
   "language": "python",
   "name": "advbox"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
